﻿using System;
using System.Data;
using System.Drawing;
using BLL;
using Model;
using ZedGraph;


public partial class StatisticData_SPCStatistic_SPCbyPIDENAID_DT : System.Web.UI.Page
{
    private DateTime starttime
    {
        get { return DateTime.Parse(ViewState["starttime"].ToString()); }
        set { ViewState["starttime"] = value; }
    }
    private DateTime stoptime
    {
        get { return DateTime.Parse(ViewState["stoptime"].ToString()); }
        set { ViewState["stoptime"] = value; }
    }
    private DataSet ds
    {
        get { return (DataSet)ViewState["ds"]; }
        set { ViewState["ds"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCustoms();
            BindProductTypeID();
            BindTestItemName();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        BindSPC();
        this.ZedGraphWeb1.Visible = true;
        this.ZedGraphWeb2.Visible = true;
        this.ZedGraphWeb3.Visible = true;
        this.ZedGraphWeb4.Visible = true;
    }

    protected void BindSPC()
    {
        string var_productTypeID = this.DropDownList1.SelectedValue;
        string var_testItemName = this.DropDownList2.SelectedItem.Text.Trim();
        string var_ENAID = this.TextBox5.Text.Trim();
        starttime = DateTime.Parse(this.TextBox3.Text.Trim() + " 00:00:00");
        stoptime = DateTime.Parse(this.TextBox4.Text.Trim() + " 00:00:00");
        TestItem_SPC spc = new TestItem_SPC();
        ds = spc.GetSPCbyENA(var_productTypeID, var_testItemName, var_ENAID, starttime, stoptime);
        this.GridView1.DataSource = ds.Tables[0];
        this.GridView1.DataBind();
        this.GridView2.DataSource = ds.Tables[1];
        this.GridView2.DataBind();
        this.GridView3.DataSource = ds.Tables[2];
        this.GridView3.DataBind();
    }

    protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindProductTypeID();
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindTestItemName();
    }

    protected void BindCustoms()
    {
        Customer cust = new Customer();
        this.DropDownList3.DataSource = cust.LoadEntities();
        this.DropDownList3.DataTextField = "CustomerName";
        this.DropDownList3.DataValueField = "CustomerID";
        this.DropDownList3.DataBind();
    }

    protected void BindProductTypeID()
    {
        ProductType pt = new ProductType();
        this.DropDownList1.DataSource = pt.GetProductTypesbyCustomerID(this.DropDownList3.SelectedValue);
        this.DropDownList1.DataTextField = "ProductTypeName";
        this.DropDownList1.DataValueField = "ProductTypeID";
        this.DropDownList1.DataBind();
    }

    protected void BindTestItemName()
    {
        TestItem ti = new TestItem();
        this.DropDownList2.DataSource = ti.GetTestItemNames(this.DropDownList1.SelectedValue);
        this.DropDownList2.DataTextField = "TestItemName";
        this.DropDownList2.DataBind();
    }

    protected void GridView3_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
    {
        GridView3.PageIndex = e.NewPageIndex;
        this.GridView3.DataSource = ds.Tables[2];
        this.GridView3.DataBind();
    }

    protected void ZedGraphWeb1_RenderGraph(ZedGraph.Web.ZedGraphWeb webObject, System.Drawing.Graphics g, ZedGraph.MasterPane pane)
    {
        GraphPane myPane = pane[0];

        myPane.Title.Text = "最大值的均值图";
        myPane.XAxis.Title.Text = "抽样点";
        myPane.YAxis.Title.Text = "";

        //创建曲线
        PointPairList list = new PointPairList();
        for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
        {
            if (!(ds.Tables[1].Rows[i][0] is DBNull))
            {
                double y = Convert.ToDouble(ds.Tables[1].Rows[i][0]);
                list.Add(i, y);
            }
        }
        if (!(ds.Tables[0].Rows[0][0] is DBNull))
        {
            PointPairList list1 = new PointPairList();
            for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
            {
                double y = Convert.ToDouble(ds.Tables[0].Rows[0][0]);
                list1.Add(i, y);
            }
            LineItem myCurve1 = myPane.AddCurve("", list1, Color.Green, SymbolType.None);
        }
        if (!(ds.Tables[0].Rows[0][4] is DBNull))
        {
            PointPairList list2 = new PointPairList();
            for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
            {
                double y = Convert.ToDouble(ds.Tables[0].Rows[0][4]);
                list2.Add(i, y);
            }
            LineItem myCurve2 = myPane.AddCurve("", list2, Color.Red, SymbolType.None);
        }
        if (!(ds.Tables[0].Rows[0][5] is DBNull))
        {
            PointPairList list3 = new PointPairList();
            for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
            {
                double y = Convert.ToDouble(ds.Tables[0].Rows[0][5]);
                list3.Add(i, y);
            }
            LineItem myCurve3 = myPane.AddCurve("", list3, Color.Red, SymbolType.None);
        }       

        LineItem myCurve = myPane.AddCurve("", list, Color.Black, SymbolType.Square); 

        string[] labels = new string[25];
        //给x轴上的每一个间隔点加上说明文字
        for (int j = 0; j < 25; j++)
        {
            labels[j] = (j + 1).ToString();
            j++;
        }

        myPane.XAxis.Scale.TextLabels = labels;

        myPane.XAxis.Type = AxisType.Text;
        MyGraphStyle.BuildLineItem(myCurve);
        MyGraphStyle.BuildbarGraphStyleforSPC(myPane);
        pane.AxisChange(g);
    }

    protected void ZedGraphWeb2_RenderGraph(ZedGraph.Web.ZedGraphWeb webObject, System.Drawing.Graphics g, ZedGraph.MasterPane pane)
    {
        GraphPane myPane = pane[0];

        myPane.Title.Text = "最小值的均值图";
        myPane.XAxis.Title.Text = "抽样点";
        myPane.YAxis.Title.Text = "";

        //创建曲线     
        PointPairList list = new PointPairList();
        for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
        {
            if (!(ds.Tables[1].Rows[i][2] is DBNull))
            {
                double y = Convert.ToDouble(ds.Tables[1].Rows[i][2]);
                list.Add(i, y);
            }
        }
        if (!(ds.Tables[0].Rows[0][2] is DBNull))
        {
            PointPairList list1 = new PointPairList();
            for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
            {
                double y = Convert.ToDouble(ds.Tables[0].Rows[0][2]);
                list1.Add(i, y);
            }
            LineItem myCurve1 = myPane.AddCurve("", list1, Color.Green, SymbolType.None);
        }
        if (!(ds.Tables[0].Rows[0][8] is DBNull))
        {
            PointPairList list2 = new PointPairList();
            for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
            {
                double y = Convert.ToDouble(ds.Tables[0].Rows[0][8]);
                list2.Add(i, y);
            }
            LineItem myCurve2 = myPane.AddCurve("", list2, Color.Red, SymbolType.None);
        }
        if (!(ds.Tables[0].Rows[0][9] is DBNull))
        {
            PointPairList list3 = new PointPairList();
            for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
            {
                double y = Convert.ToDouble(ds.Tables[0].Rows[0][9]);
                list3.Add(i, y);
            }
            LineItem myCurve3 = myPane.AddCurve("", list3, Color.Red, SymbolType.None);
        }

        LineItem myCurve = myPane.AddCurve("", list, Color.Black, SymbolType.Square); 

        string[] labels = new string[25];
        //给x轴上的每一个间隔点加上说明文字
        for (int j = 0; j < 25; j++)
        {
            labels[j] = (j + 1).ToString();
            j++;
        }

        myPane.XAxis.Scale.TextLabels = labels;

        myPane.XAxis.Type = AxisType.Text;
        MyGraphStyle.BuildLineItem(myCurve);
        MyGraphStyle.BuildbarGraphStyleforSPC(myPane);
        pane.AxisChange(g);
    }

    protected void ZedGraphWeb3_RenderGraph(ZedGraph.Web.ZedGraphWeb webObject, System.Drawing.Graphics g, ZedGraph.MasterPane pane)
    {
        GraphPane myPane = pane[0];

        myPane.Title.Text = "最大值的极差图";
        myPane.XAxis.Title.Text = "抽样点";
        myPane.YAxis.Title.Text = "";

        //创建曲线
        PointPairList list = new PointPairList();
        for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
        {
            if (!(ds.Tables[1].Rows[i][1] is DBNull))
            {
                double y = Convert.ToDouble(ds.Tables[1].Rows[i][1]);
                list.Add(i, y);
            }
        }
        if (!(ds.Tables[0].Rows[0][1] is DBNull))
        {
            PointPairList list1 = new PointPairList();
            for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
            {
                double y = Convert.ToDouble(ds.Tables[0].Rows[0][1]);
                list1.Add(i, y);
            }
            LineItem myCurve1 = myPane.AddCurve("", list1, Color.Green, SymbolType.None);
        }
        if (!(ds.Tables[0].Rows[0][6] is DBNull))
        {
            PointPairList list2 = new PointPairList();
            for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
            {
                double y = Convert.ToDouble(ds.Tables[0].Rows[0][6]);
                list2.Add(i, y);
            }
            LineItem myCurve2 = myPane.AddCurve("", list2, Color.Red, SymbolType.None);
        }
        if (!(ds.Tables[0].Rows[0][7] is DBNull))
        {
            PointPairList list3 = new PointPairList();
            for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
            {
                double y = Convert.ToDouble(ds.Tables[0].Rows[0][7]);
                list3.Add(i, y);
            }
            LineItem myCurve3 = myPane.AddCurve("", list3, Color.Red, SymbolType.None);
        }

        LineItem myCurve = myPane.AddCurve("", list, Color.Black, SymbolType.Square); 

        string[] labels = new string[25];
        //给x轴上的每一个间隔点加上说明文字
        for (int j = 0; j < 25; j++)
        {
            labels[j] = (j + 1).ToString();
            j++;
        }

        myPane.XAxis.Scale.TextLabels = labels;

        myPane.XAxis.Type = AxisType.Text;
        MyGraphStyle.BuildLineItem(myCurve);
        MyGraphStyle.BuildbarGraphStyleforSPC(myPane);
        pane.AxisChange(g);
    }

    protected void ZedGraphWeb4_RenderGraph(ZedGraph.Web.ZedGraphWeb webObject, System.Drawing.Graphics g, ZedGraph.MasterPane pane)
    {
        GraphPane myPane = pane[0];

        myPane.Title.Text = "最小值的极差图";
        myPane.XAxis.Title.Text = "抽样点";
        myPane.YAxis.Title.Text = "";

        //创建曲线
        PointPairList list = new PointPairList();
        for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
        {
            if (!(ds.Tables[1].Rows[i][3] is DBNull))
            {
                double y = Convert.ToDouble(ds.Tables[1].Rows[i][3]);
                list.Add(i, y);
            }
        }
        if (!(ds.Tables[0].Rows[0][3] is DBNull))
        {
            PointPairList list1 = new PointPairList();
            for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
            {
                double y = Convert.ToDouble(ds.Tables[0].Rows[0][3]);
                list1.Add(i, y);
            }
            LineItem myCurve1 = myPane.AddCurve("", list1, Color.Green, SymbolType.None);
        }
        if (!(ds.Tables[0].Rows[0][10] is DBNull))
        {
            PointPairList list2 = new PointPairList();
            for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
            {
                double y = Convert.ToDouble(ds.Tables[0].Rows[0][10]);
                list2.Add(i, y);
            }
            LineItem myCurve2 = myPane.AddCurve("", list2, Color.Red, SymbolType.None);
        }
        if (!(ds.Tables[0].Rows[0][11] is DBNull))
        {
            PointPairList list3 = new PointPairList();
            for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
            {
                double y = Convert.ToDouble(ds.Tables[0].Rows[0][11]);
                list3.Add(i, y);
            }
            LineItem myCurve3 = myPane.AddCurve("", list3, Color.Red, SymbolType.None);
        }

        LineItem myCurve = myPane.AddCurve("", list, Color.Black, SymbolType.Square); 

        string[] labels = new string[25];
        //给x轴上的每一个间隔点加上说明文字
        for (int j = 0; j < 25; j++)
        {
            labels[j] = (j + 1).ToString();
            j++;
        }

        myPane.XAxis.Scale.TextLabels = labels;

        myPane.XAxis.Type = AxisType.Text;
        MyGraphStyle.BuildLineItem(myCurve);
        MyGraphStyle.BuildbarGraphStyleforSPC(myPane);
        pane.AxisChange(g);
    }
}
